package edu.mis.modules.commodity;

import java.util.List;

import com.jfinal.plugin.activerecord.Record;

import edu.mis.model.Commodity;
import edu.mis.model.Customer;
import edu.mis.model.User;

/**
 * 权限服务类
 *
 */
public class CommodityService {
	
	/**
	 * 添加新商品
	 * @param user
	 * @return
	 */
	public boolean saveCommodity(Commodity commodity){
		if(commodity.findCommodityByName(commodity.getName())==null){ 
			return commodity.save();
		}else{
			return false;
		}
		
	}
	
	/**
	 * 修改商品信息
	 * @param id
	 * @return
	 */
	public boolean updateCommodity(int id,int typeId,double price){
		Commodity commodity = findCommodityById(id);
		commodity.setTypeId(typeId);
		commodity.setPrice(price);
		return commodity.update();
	}
	
	
	/**
	 * 根据ID得到商品
	 * @param id
	 * @return
	 */
	public Commodity findCommodityById(int id) {
		return Commodity.dao.findById(id);
	}
	
	/**
	 * 查询所有用户
	 * @return
	 */
	public List<Commodity> findAllCommodity(){
		return Commodity.dao.findAll();
	}
	/**
	 * 查询所有商品及其权限
	 * @return
	 */
	public List<Record> findAllCommodityWithCommodtype(){
		return Commodity.dao.findAllRecord();
	}

	/**
	 * 用户授权
	 * @param id
	 * @param roleId
	 * @return
	 */
	public boolean commodityAuthorize(int id, int typeId) {
		Commodity commodity = findCommodityById(id);
		commodity.setTypeId(typeId);
		return commodity.update();
	}

	
	public boolean deleteCommodityById(int id) {
		return Commodity.dao.deleteById(id);
	}


}
